package com.yzt.zhuju.dialog;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;


import com.yzt.zhuju.R;
import com.yzt.zhuju.widget.tuiview.TUITextView;

/**
 * author : bob
 * date   : 2022/12/9
 * desc   : 聊天室管理员操作面板
 */
public class ChatRoomMasterActionDialog extends NewBaseBottomDialog implements View.OnClickListener {

    public static final String DIALOG_RESULT_LOOK_INFO = "result_look_info";
    public static final String DIALOG_RESULT_SET_TO_LEAVE_ROOM = "result_set_to_leave_room";
    public static final String DIALOG_RESULT_MESSAGE_MUTE = "message_mute";

    private boolean mCanSendMessage;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getArgs();
        View view = inflater.inflate(R.layout.dialog_chat_room_master_action, container, false);
        initView(view);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        Window window = dialog.getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            WindowManager.LayoutParams params = window.getAttributes();
            params.gravity = Gravity.BOTTOM;
            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
            params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            window.setAttributes(params);
        }
        dialog.setCanceledOnTouchOutside(true);
    }

    /**
     * 解析参数
     */
    private void getArgs() {
        Bundle masterActionBundle = getArguments();
        mCanSendMessage = masterActionBundle.getBoolean("mCanSendMessage");
    }

    private void initView(View view) {
        TextView tvActionSetToLeaveRoom = view.findViewById(R.id.tv_action_set_to_leave_room);
        TextView tvActionLookInfo = view.findViewById(R.id.tv_action_look_info);
        TextView tvActionSpeakForbid = view.findViewById(R.id.tv_action_speak_forbid);
        TUITextView tvActionCancel = view.findViewById(R.id.tv_action_cancel);

        updateMessageMuteUi(tvActionSpeakForbid);

        tvActionSetToLeaveRoom.setOnClickListener(this);
        tvActionLookInfo.setOnClickListener(this);
        tvActionSpeakForbid.setOnClickListener(this);
        tvActionCancel.setOnClickListener(this);
    }

    private void updateMessageMuteUi(TextView tv) {
        tv.setVisibility(View.VISIBLE);
        if (mCanSendMessage) {
            tv.setText(getString(R.string.cancel_muted));
        } else {
            tv.setText(getString(R.string.muted));
        }
    }

    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.tv_action_set_to_leave_room) {
            // 踢出房间
            mDialogFragmentResultListener.confirm(DIALOG_RESULT_SET_TO_LEAVE_ROOM);
        } else if (viewId == R.id.tv_action_look_info) {
            // 查看资料
            mDialogFragmentResultListener.confirm(DIALOG_RESULT_LOOK_INFO);
        } else if (viewId == R.id.tv_action_speak_forbid) {
            // 禁言
            mDialogFragmentResultListener.confirm(DIALOG_RESULT_MESSAGE_MUTE);
        }
        dismiss();
    }
}

